<!--
 @description：发起点餐任务
 @author: Xg
 @date: 2021/6/25 17:01
-->
<template>
  <el-dialog
    :title="title"
    :visible="startOrderDialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="closeDialog(false)"
  >
    <el-form label-width="120px" :model="taskParams">
      <el-form-item label="点餐标题">
        <el-input v-model="defaultTaskTitle" class="task-title" />
      </el-form-item>
      <el-form-item label="选择点餐商家">
        <el-select v-model="taskParams.storeId" placeholder="请选择" size="small" class="select-store">
          <el-option
            v-for="(item, index) in storeList"
            :key="index"
            :label="item.storeName"
            :value="item.storeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="截止时间">
        <el-time-picker
          v-model="endTime"
          style="width: 80%"
          placeholder="选择时间"
          class="date-picker"
          format="HH:mm"
          size="small"
          type="datetime"
          @change="change"
        />
      </el-form-item>
      <br>
      <div class="dialog-footer" align="right">
        <el-button size="small" @click="closeDialog(false)">取消</el-button>
        <el-button size="small" type="success" @click="generatorTask()">发起点餐任务</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>

import { list } from '@/api/basic/store'
import { generatorTask } from '@/api/meal/task'

export default {
  name: 'StartOrderDialog',
  props: {
    startOrderDialogVisible: {
      type: Boolean,
      default: false
    },
    defaultTaskTitle: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      title: '发起点餐',
      // 商家信息
      storeList: [],
      taskParams: {
        endTime: null,
        storeId: null,
        taskTitle: null
      },
      endTime: null,
      isRefresh: false
    }
  },
  mounted() {
    this.getStoreList()
  },
  methods: {
    // 时间改变
    change() {
      this.taskParams.endTime = this.endTime
    },
    // 关闭页面
    closeDialog(isRefresh) {
      this.endTime = null
      this.taskParams.endTime = null
      this.taskParams.storeId = this.storeList[0].storeId
      this.$emit('close', isRefresh)
    },
    // 获取所有商家信息
    getStoreList() {
      list().then(res => {
        if (res.code === 200) {
          this.storeList = res.data
          if (this.storeList.length) {
            this.taskParams.storeId = this.storeList[0].storeId
          }
        }
      })
    },
    // 发起任务
    generatorTask() {
      this.taskParams.taskTitle = this.defaultTaskTitle
      generatorTask(this.taskParams).then(res => {
        if (res.code === 200) {
          this.$message.success('保存成功，请在主页查看任务码!')
          this.closeDialog(true)
        }
      })
    }
  }
}
</script>

<style scoped>
  .date-picker {
    margin-right: 90%;
  }
  .task-title {
    width: 80%;
  }
  .select-store {
    width: 80%;
  }
</style>
